﻿#pragma once
#include<iostream>
#include"RBTree.h"

namespace zh
{
	template<class K,class V>
	class map
	{
		struct mapKeyOf
		{
			const K& operator()(const pair<K,V>& p)
			{
				return p.first;
			}
		};
	public:
		typedef typename BRTree<K, pair<K, V>, mapKeyOf>::Iterator iterator;
		typedef typename BRTree<K, pair<K, V>, mapKeyOf>::const_Iterator const_iterator;

		pair<iterator,bool> insert(const pair<K, V>& data)
		{
			return _t.Insert(data);
		}


		iterator begin()
		{
			return _t.Begin();
		}

		iterator end()
		{
			return _t.End();
		}

		const_iterator begin() const 
		{
			return _t.Begin();
		}
		const_iterator end() const
		{
			return _t.End();
		}

		iterator find(const K& data)
		{
			return _t.Find(data);
		}
		V& operator[](const K& key)
		{
			pair<iterator,bool> ret =  _t.Insert(make_pair(key,V()));
			return ret.first->second;
		}
	private:
		BRTree<K, pair<K,V>,mapKeyOf> _t;
	};
}
void maptest()
{
	zh::map<int, int> m;
	m.insert({ 1,2 });
	m.insert({ 2,4 });
	m.insert({ 5,6 });
	m.insert({ 3,6 });
	auto it = m.begin();
	//while (it != m.end())
	//{
	//	cout << (*it).first << "," << (*it).second << endl;
	//	++it;
	//}
	m.find(1);
}
void test_map2()
{
	string arr[] = { "草莓", "西瓜", "草莓", "西瓜", "西瓜", "西瓜", "香蕉" };
	zh::map<string, int> countMap;
	for (auto& e : arr)
	{
		countMap[e]++;
	}

	for (auto& kv : countMap)
	{
		cout << kv.first << ":" << kv.second << endl;
	}
	cout << endl;
}
